/*
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

@title: Streetblink
@description: Streetblink is a game where you drive a car down a road and try to avoid obstacles. Every 3 seconds you can point your car in a new direction and see where it ended up.
@author: 2PBHD
@tags: []
@addedOn: 2024-11-01
*/
let gray = bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL`
let line = bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
22222222LLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL`
let side = bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
4444444444444444
4444444444444444
4444444444444444
4444444444444444`


let title = [
  bitmap`
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777333336666666
7773333336333333
7773333333333333`,
  bitmap`
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
6666666666666773
3333333333333773
3333333333333773`,
  bitmap`
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
3333333666666666
3333333633333333
3333333333333333`,
  bitmap`
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
6666666667733333
3333333337733333
3333333337733333`,
  bitmap`
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
3333366666666666
3333363333333333
3333333333333333`,
  bitmap`
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
6666777733333333
3333377733333333
3333377733333333`,
  bitmap`
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
3666666666667733
3633333333337733
3333333333337733`,
  bitmap`
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
3333333366666666
3333333363333333
3333333333333333`,
  bitmap`
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
6677333333333666
3377333333333633
3377333333333333`,
  bitmap`
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
6666666666666677
3333333333333377
3333333333333377`,

  bitmap`
7773333333333333
7773333333333333
7773333333333333
7773333333333333
7773333333777777
7773333333777777
7773333333777777
7773333333777777
7773333333777777
7773333333777777
7773333333777777
7773333333333333
7773333333333333
7773333333333333
7773333333333333
7773333333333333`,
  bitmap`
3333333333333773
3333333333333773
3333333333333773
3333333333333773
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
3333333333337777
3333333333333777
3333333333333777
3333333333333777
3333333333333777`,
  bitmap`
3333333333333333
3333333333333333
3333333333333333
3333333333333333
7777777333333333
7777777333333333
7777777333333333
7777777333333333
7777777333333333
7777777333333333
7777777333333333
7777777333333333
7777777333333333
7777777333333333
7777777333333333
7777777333333333`,
  bitmap`
3333333337733333
3333333337733333
3333333337733333
3333333337733333
3777777777733333
3777777777733333
3777777777733333
3777777777733333
3777777777733333
3777777777733333
3777777777733333
3777777777733333
3777777777733333
3777777777733333
3777777777733333
3777777777733333`,
  bitmap`
3333333333333333
3333377777777777
3333377777777777
3333377777777777
3333377777777777
3333377777777777
3333377777777777
3333377777777777
3333377777777777
3333377777777777
3333377777777777
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333377777733333`,
  bitmap`
3333377733333333
7333377733333333
7333377733333333
7333377733333333
7333377733333333
7333377733333333
7333377733333333
7333377733333333
7333377733333333
7333377733333333
7333377733333333
3333377733333333
3333377733333333
3333377733333333
3333377733333333
3377777733333333`,
  bitmap`
3333333333337733
3333333333337733
3333333333337733
3333333333337733
3777777777777733
3777777777777733
3777777777777733
3777777777777733
3777777777777733
3777777777777733
3777777777777733
3333333333337733
3333333333337733
3333333333337733
3333333333337733
3333333333337733`,
  bitmap`
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333777777777
3333333777777777
3333333777777777
3333333777777777
3333333777777777
3333333777777777
3333333777777777
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333`,
  bitmap`
3377333333333333
3377333333333333
3377333333333333
3377333333333333
7777777777773333
7777777777773333
7777777777773333
7777777777773333
7777777777773333
7777777777773333
7777777777773333
3377777777773333
3377777777773333
3377777777773333
3377777777773333
3377777777773333`,
  bitmap`
3333333333333377
3333333333333377
3333333333333377
3333333333333377
3333337777777777
3333337777777777
3333337777777777
3333337777777777
3333337777777777
3333337777777777
3333337777777777
3333337777777777
3333337777777777
3333337777777777
3333337777777777
3333337777777777`,

  bitmap`
7773333333333333
7777333333333333
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7773333333333333
7773333333333333
7773333333333333
7773333333333333
4443333333333333
4443333333333333
4446666666666666
4444444444444444`,
  bitmap`
3333333333333777
3333333333333777
7777773333333777
7777773333333777
7777773333333777
7777773333333777
7777773333333777
7777773333333777
3333333333333777
3333333333333777
3333333333333777
3333333333333777
3333333333333444
3333336333333444
6666666333334444
4444444444444444`,
  bitmap`
7777777333333333
7777777333333333
7777777333333333
7777777333333333
7777777333333333
7777777333333333
7777777333333333
7777777333333333
7777777333333333
7777777333333333
7777777333333333
7777777333333333
4444444333333333
4444444333333333
4444444333333363
4444444666666663`,
  bitmap`
3777777777733333
3777777777733333
3777777777733333
3777777777733333
3777777777733333
3777777777733333
3777777777733333
3777777777733333
3777777777733333
3777777777733333
3777777777733333
3777777777733333
3444444444433333
3444444444433333
3444444444433333
3444444444466666`,
  bitmap`
3333377777733333
3333377777733333
3333377777733333
3333377777733333
3333377777733333
3333377777733333
3333377777733333
3333377777733333
3333377777733333
3333377777733333
3333377777733333
3333377777733333
3333344444433333
3333344444433333
3363344444433333
6663344444466666`,
  bitmap`
3377777733333333
3377777733333333
3377777733333333
3377777733333333
3337777733333333
3337777733333333
3337777733333333
3337777733333333
3337777733333333
3337777733333333
3337777733333333
3337777733333333
3334444433333333
3334444433333333
6334444433333333
6334444466666666`,
  bitmap`
3333333333337733
3333333333337733
3777777777777733
3777777777777733
3777777777777733
3777777777777733
3777777777777733
3777777777777733
3777777777777733
3333333333337733
3333333333337733
3333333333337733
3333333333334433
3333333333334433
3633333333334433
6633333333334466`,
  bitmap`
3333333333333333
3333333333333333
3333333777777777
3333333777777777
3333333777777777
3333333777777777
3333333777777777
3333333777777777
3333333777777777
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333633333333
6666666633333333`,
  bitmap`
3377777777773333
3377777777773333
7777777777773333
7777777777773333
7777777777773333
7777777777773333
7777777777773333
7777777777773333
7777777777773333
3377777777773333
3377777777773333
3377777777773333
3344444444443333
3344444444443333
3344444444443333
3344444444446666`,
  bitmap`
3333337777777777
3333337777777777
3333337777777777
3333337777777777
3333337777777777
3333337777777777
3333337777777777
3333337777777777
3333337777777777
3333337777777777
3333337777777777
3333337777777777
3333334444444444
3333334444444444
3633334444444444
6633334444444444`,

  bitmap`
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL`,
  bitmap`
4444444444444444
4441111111111111
4411111111111111
4111171111111111
4171111111111111
4171111111111111
4111111444444444
4111711444444444
4111111444444444
4111111444444444
L111111LLLLLLLLL
L111111LLLLLLLLL
L111111LLLLLLLLL
L171711111111111
L171111111111111
L171111111111111`,
  bitmap`
4444444444444444
1111111444441111
1111111144411111
1111111114411111
1111111114417171
1111111114417111
4441111114411111
4441111114411111
4441111114411111
4441111114411117
LLL111111LL17111
LLL111111LL17111
LLL111111LL11111
111111111LL11171
11111111LLL11111
1111111LLLL17111`,
  bitmap`
4444444444444444
1444444444444444
1144444444444444
1144444444444444
1144444444444444
1144444444444444
1144444444444444
1144444444444444
1144444444444444
1144444444444444
11LLLLLLLLLLLLLL
11LLLLLLLLLLLLLL
11LLLLLLLLLLLLLL
11LLLLLLLLLLLLLL
11LLLLLLLLLLLLLL
11LLLLLLLLLLLLLL`,
  bitmap`
4444444444444444
4444411111111111
4444111711111111
4444111111111111
4444171111111111
4444171111111111
4444111117111111
4444411111111111
4444444444444111
4444444444444111
LLLLLLLLLLLLL111
LLLLLLLLLLLLL111
LLLLLLLLLLLLL111
LLLLLLLLLLLLL111
LLLLLLLLLLLLL111
LLLLLLLLLLLLL111`,
  bitmap`
4444444444444444
1111111111114444
1111111111111441
1111111111111441
1111111111111441
1111111111111441
1111111111111441
1111111111114441
1111144444444441
1711144444444441
11111LLLLLLLLLL1
11111LLLLLLLLLL1
11111LLLLLLLLLL1
11111LLLLLLLLLL1
11111LLLLLLLLLL1
11711LLLLLLLLLL1`,
  bitmap`
4444444444444444
1111111114444444
1171111114444441
1111111111444441
7111111111444441
7111111111444441
1111111111144441
1111711111144441
1111111111144441
1111111111114441
117111111111LLL1
711111111111LLL1
7111111111111LL1
1111111111111LL1
1111111L11111LL1
1111171L111111L1`,
  bitmap`
4444444444444444
1111114444111111
1111111441111111
1111111441711711
1111111441711111
1111111441111111
1111111441111111
1111111441111111
1111111441111111
1111111441111117
1111111LL1111111
1111111LL1111111
1111111LL1711111
1111111LL1711111
1111111LL1717111
1111111LL1111111`,
  bitmap`
4444444444444444
4444444444444441
1444444444444111
1444444444411111
1444444441111111
1444444111111111
1444411111111111
1441111111111114
1111111111111444
1111111111144444
111111111LLLLLLL
1111111LLLLLLLLL
11111LLLLLLLLLLL
111LLLLLLLLLLLLL
1111111LLLLLLLLL
111111111LLLLLLL`,
  bitmap`
4444444444444444
1444444444444444
1144444444444444
1144444444444444
1144444444444444
1144444444444444
1444444444444444
4444444444444444
4444444444444444
4444444444444444
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL`,

  gray,
  bitmap`
L111111111111111
L111171111111111
L111111111111111
L111111LLLLLLLLL
L171111LLLLLLLLL
L171111LLLLLLLLL
L111111LLLLLLLLL
L111711LLLLLLLLL
L111111LLLLLLLLL
L111111111111111
L111171111111111
L171111111111111
L177111111111111
L111111111111111
LL11111111111111
LLLLLLLLLLLLLLLL`,
  bitmap`
11111111LLL17111
111111111LL11111
111111111LL11111
LLL111111LL11111
LLL111111LL11117
LLL111111LL11111
LLL111111LL17111
LLL111111LL17111
LLL111111LL11171
111111111LL11111
111111111LL11111
111111111LL17111
111111111LL17111
11111111LLL11111
1111111LLLLL1111
LLLLLLLLLLLLLLLL`,
  bitmap`
11LLLLLLLLLLLLLL
11LLLLLLLLLLLLLL
11LLLLLLLLLLLLLL
11LLLLLLLLLLLLLL
11LLLLLLLLLLLLLL
11LLLLLLLLLLLLLL
11LLLLLLLLLLLLLL
11LLLLLLLLLLLLLL
1111111111111111
1111111111111111
1111111111111111
7111111111111111
1111111111111111
1111111111111111
1111111111111111
LLLLLLLLLLLLLLLL`,
  bitmap`
LLLLLLLLLLLLL171
LLLLLLLLLLLLL171
LLLLLLLLLLLLL111
LLLLLLLLLLLLL111
LLLLLLLLLLLLL111
LLLLLLLLLLLLL171
LLLLLLLLLLLLL171
LLLLLLLLLLLLL111
11LLLL1111111111
111LL11111111111
111LL11111711111
111LL17111111111
111LL17111111111
111LL11111111111
11LLLL1111111111
LLLLLLLLLLLLLLLL`,
  bitmap`
11111LLLLLLLLLL1
11111LLLLLLLLLL1
11111LLLLLLLLLL1
17111LLLLLLLLLL1
11111LLLLLLLLLL1
11111LLLLLLLLLL1
11171LLLLLLLLLL1
11111LLLLLLLLLL1
111111111111LLL1
1111111111111LL1
1111111111111LL1
1111111111111LL1
1111111111111LL1
1111111111111LL1
111111111111LLLL
LLLLLLLLLLLLLLLL`,
  bitmap`
1111111LL11111L1
1111111LL1111111
7117111LL1111111
7111111LLL111111
7111111LLL111111
1111111LLL111111
1111111LLLL11111
1111711LLLL11111
1111111LLLL11111
1111111LLLLL1111
1111111LLLLL1111
7171111LLLLL1111
7111111LLLLLL111
1111111LLLLLL111
111111LLLLLLLL11
LLLLLLLLLLLLLLLL`,
  bitmap`
1111111LL1111111
1111111LL1111111
1111111LL1111111
1111111LL1111171
1111111LL1111111
1111111LL1111111
1111111LL1711111
1111111LL1711111
1111111LL1117111
1111111LL1111111
1111111LL1111711
1111111LL1711111
1111111LL1717111
1111111LL1111111
111111LLLL111111
LLLLLLLLLLLLLLLL`,
  bitmap`
1111111111LLLLLL
11111111111LLLLL
111111111111LLLL
111111111111LLLL
1LLLL11111111LLL
1LLLLL1111111LLL
1LLLLLL111111LLL
1LLLLLLL111111LL
1LLLLLLL111111LL
1LLLLLLLL111111L
1LLLLLLLL111111L
1LLLLLLLLL11111L
1LLLLLLLLL11111L
1LLLLLLLLL11111L
LLLLLLLLLLL111LL
LLLLLLLLLLLLLLLL`,
  gray,

  bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLL22222222L
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL`,
  bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLL000
LLLLLLLLLLLLL000
LLLLLLLLLL000000
LLLLLLLLL0LLLLLL
LLLLLLLL0LLLLLLL
LLLLLLL0LLLLLLLL
LLLLLLL0LLLLLLLL
LLLLLL0LLLLLLLLL
LLLLLL0LLLLLLLLL
LLLLLL0LLLLLLLLL
LLLLL0LLLLLLLLLL
LLLLL0LLLLLLLLLL`,
  bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
000000000LLLLLLL
000000000LLLLLLL
0000000000000000
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLL77777
LLLLLLLLLLL77777
LLLLLLLLLLL77777
LLLLLLLLLLL77777
LLLLLLLLLLL77777
LLLLLLLLLLL77777`,
  bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
0000000000000000
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
777777777LLLLLLL
777777777LLLLLLL
777777777LLLLLLL
777777777LLLLLLL
777777777LLLLLLL
777777777LLLLLLL`,
  bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLL0000000
0000000000000000
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL`,
  bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
00000LLLLLLLLLLL
00000000LLLLLLLL
LLLLLLLL0LLLLLLL
LLLLLLLLL0LLLLLL
LLLLLLLLLL0LLLLL
LLLLLLLLLLL0LLLL
LLLLLLLLLLL0LLLL
LLLLLLLLLLLL0LLL
LLLLLLLLLLLL0LLL
LLLLLLLLLLLL0LLL
LLLLLLLLLLLLL0LL`,
  line,
  line,
  line,
  line,

  gray,
  bitmap`
LLLLL0LLLLLLLLLL
LLLLL01111111111
LLLLL01111111111
LLLLL01111111111
LLLLL01111111111
LLLLL01111111111
LLLLL01111111111
LLLLL01111111111
LLLLL01111111111
LLLLL01111111111
LLLLL0LLLLLLLLLL
LLLLL0LLLLLLLLLL
LLLLL0LLLLLLLLLL
LLLLLL0LLLLLLLLL
LLLLLL0LLLLLLLLL
LLLLLL0LLLLLLLLL`,
  bitmap`
LLLLLLLLLLL77777
1111111111177777
1111111111177777
1111111111177777
1111111111177777
1111111111177777
1111111111177777
1111111111177777
1111111111177777
1111111111177777
LLLLLLLLLLL77777
LLLLLLLLLLL77777
LLLLLLLLLLL77777
LLLLLLLLLLL77777
LLLLLLLLLLL77777
LLLLLLLLLLL77777`,
  bitmap`
777777777LLLLLLL
7777777771111111
7777777771111111
7777777771111111
7777777771111111
7777777771111111
7777777771111111
7777777771111111
7777777771111111
7777777771111111
777777777LLLLLLL
777777777LLLLLLL
777777777LLLLLLL
777777777LLLLLLL
777777777LLLLLLL
777777777LLLLLLL`,
  bitmap`
LLLLLLLLLLLLLLLL
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL`,
  bitmap`
LLLLLLLLLLLLL0LL
11111111111110LL
11111111111110LL
11111111111110LL
11111111111110LL
11111111111110LL
11111111111110LL
11111111111110LL
11111111111110LL
11111111111110LL
LLLLLLLLLLLLL0LL
LLLLLLLLLLLLL0LL
LLLLLLLLLLLL0LLL
LLLLLLLLLLLL0LLL
LLLLLLLLLLLL0LLL
LLLLLLLLLLL0LLLL`,
  gray,
  gray,
  gray,
  gray,

  side,
  bitmap`
LLLLLLL0LLLLLLLL
LLLLLLL0LLLLLLLL
LLLLLLLL0LLLLLLL
LLLLLLLLL0LLLLLL
LLLLLLLLLL000000
LLLLLLLLLLLLL000
LLLLLLLLLLLLL000
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
4444444444444444
4444444444444444
4444444444444444
4444444444444444`,
  bitmap`
LLLLLLLLLLL77777
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
0000000000000000
000000000LLLLLLL
000000000LLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
4444444444444444
4444444444444444
4444444444444444
4444444444444444`,
  bitmap`
777777777LLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
0000000000000000
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
4444444444444444
4444444444444444
4444444444444444
4444444444444444`,
  bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
0000000000000000
LLLLLLLLLL000000
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
4444444444444444
4444444444444444
4444444444444444
4444444444444444`,
  bitmap`
LLLLLLLLLLL0LLLL
LLLLLLLLLL0LLLLL
LLLLLLLLL0LLLLLL
LLLLLLLL0LLLLLLL
00000000LLLLLLLL
00000LLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
4444444444444444
4444444444444444
4444444444444444
4444444444444444`,
  side,
  side,
  side,
  side
]
let titles = []
for (let i = 0; i < 8; i++) {
  out = bitmapToArray(title[i * 10])
  for (let j = 1; j < 10; j++) {
    let tmp = bitmapToArray(title[i * 10 + j])
    for (let k = 0; k < 16; k++) {
      out[k] += tmp[k]
    }
  }
  titles = titles.concat(out)
}

const rythmy = tune`
500: G4~500 + D4~500,
500: G4~500 + D4~500,
500: G4~500 + D4~500,
500: G4~500 + D4~500,
500: G4~500 + D4~500,
500: G4~500 + D4~500,
500: G4~500 + D4~500,
500: G4~500 + D4~500,
500: G4~500 + E4~500,
500: G4~500 + E4~500,
500: G4~500 + E4~500,
500: G4~500 + E4~500,
500: G4~500 + E4~500,
500: G4~500 + E4~500,
500: G4~500 + E4~500,
500: G4~500 + E4~500,
500: G4~500 + F4~500,
500: G4~500 + F4~500,
500: G4~500 + F4~500,
500: G4~500 + F4~500,
500: G4~500 + F4~500,
500: G4~500 + F4~500,
500: G4~500 + F4~500,
500: G4~500 + F4~500,
500: G4~500 + C4~500,
500: G4~500 + C4~500,
500: G4~500 + C4~500,
500: G4~500 + C4~500,
500: G4~500 + C4~500,
500: G4~500 + C4~500,
500: G4~500 + C4~500,
500: G4~500 + C4~500`
const melody = [
  tune`
500: G5^500,
500: G5^500,
500: G5^500,
500: E5^500,
500: A5^500,
1500,
500: A5^500,
500: A5^500,
500: A5^500,
500: G5^500,
500: B5^500,
1500,
500: F5^500,
500: D5^500,
500: G5^500,
500: F5^500,
500: E5^500,
500: C5^500,
500: B4^500,
500: A5^500,
500: G5^500,
500: F5^500,
500: E5^500,
500: D5^500,
500: B5^500,
500,
500: B5^500,
500`,
  tune`
500: C5/500,
500: A4/500,
500: C5/500,
500: A4/500,
500: C5/500,
500: A4/500,
500: C5/500,
500: A4/500,
500: D5/500,
500: A4/500,
500: D5/500,
500: A4/500,
500: E5/500,
500: A4/500,
500: E5/500,
500: A4/500,
500: F5/500,
500: F5/500,
500: F5/500,
500: F5/500,
500: G5/500,
500: F5/500,
500: E5/500,
500: D5/500,
500: E5/500,
500: D5/500,
500: C5/500,
500: C4~500 + B4/500,
500: C5/500,
500: B4/500,
500: A4/500,
500: B4/500`,
  tune`
500: B4/500 + F5/500,
500: A4/500 + E5/500,
500: B4/500 + F5/500,
500: B4/500 + G5/500,
500: B4/500 + G5/500,
500: C5/500,
500: B4/500,
500: A4/500,
500: B4/500 + F5/500,
500: A4/500 + E5/500,
500: G4/500 + D5/500,
500: A4/500,
500: B4/500,
500: C5/500,
500: C5/500,
500: C5/500,
500: B4/500,
500: A4/500,
500: D5/500,
500: C5/500,
500: B4/500,
500: B4/500,
500: A4/500,
500: G4/500,
500: A4/500,
500: A4/500,
500: A4/500,
500: A4/500,
500: C5/500,
500: A4/500,
500: A4/500,
500: G4/500`
]
let theme = 0

function playTheme() {
  playTune(rythmy)
  if (theme > 0) {
    playTune(melody[theme - 1])
  }
  theme++
  theme %= 4
  setTimeout(playTheme, 16250)
}
playTheme()

function replaceAt(string, char, index) {
  return string.substring(0, index) + char + string.substring(index + 1)
}

class cell {
  constructor(char, bitmap) {
    this.char = char
    this.bitmap = bitmap
  }

  setPixel(x, y, char) {
    let index = x + y * 16
    if (x >= 0 && x < 16 && y >= 0 && y < 16)
      this.bitmap = replaceAt(this.bitmap, char, index + (index / 16) + 1)
  }

  drawify() {
    return
  }
}

let empty = bitmap`
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD`

let baseCells = []

let bottomLines = bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
222LLLLL222LLLLL`
let topLines = bitmap`
222LLLLL222LLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL`
let bar = bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
1111111111111111
1111111111111111
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL`

function bitmapToArray(bitmap) {
  return bitmap.substring(1).split("\n")
}

let maps = [
    empty,
    bar,
    gray,
    bottomLines,
    topLines,
    gray,
    bar,
    empty,
  ]
  .reduce((a, b) => bitmapToArray(b).concat(a), [])
  .map(x => x.repeat(50))

let chars = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_=+{}[]|(;:'),<>/"
for (let i = 0; i < 10 * 8; i++) {
  baseCells.push(new cell(chars.charAt(i), empty))
}
let cells = baseCells.map(x => x)

/* you could have made it take an array (spreading is too expensive)
function generateLegend() {
  let out = ""
  for (let i = 0; i < 10 * 8; i++) {
    //log doesn't support newlines
    out += `["${chars.charAt(i)}", cells[${i}].bitmap],\n`
  }
  console.log(out)
}
generateLegend()*/
let xDrift = 0

function setCells(maps) {
  cells.forEach((cell, i) => {
    let y = Math.floor(i / 10)
    let x = i % 10
    let out = "\n"
    for (let i = 0; i < 16; i++) {
      out += maps[y * 16 + i]
        .substring(x * 16, x * 16 + 16) + "\n"
    }
    cells[i].bitmap = out
  })
}

function setScreen() {
  setLegend(
    ["1", cells[0].bitmap],
    ["2", cells[1].bitmap],
    ["3", cells[2].bitmap],
    ["4", cells[3].bitmap],
    ["5", cells[4].bitmap],
    ["6", cells[5].bitmap],
    ["7", cells[6].bitmap],
    ["8", cells[7].bitmap],
    ["9", cells[8].bitmap],
    ["0", cells[9].bitmap],
    ["a", cells[10].bitmap],
    ["b", cells[11].bitmap],
    ["c", cells[12].bitmap],
    ["d", cells[13].bitmap],
    ["e", cells[14].bitmap],
    ["f", cells[15].bitmap],
    ["g", cells[16].bitmap],
    ["h", cells[17].bitmap],
    ["i", cells[18].bitmap],
    ["j", cells[19].bitmap],
    ["k", cells[20].bitmap],
    ["l", cells[21].bitmap],
    ["m", cells[22].bitmap],
    ["n", cells[23].bitmap],
    ["o", cells[24].bitmap],
    ["p", cells[25].bitmap],
    ["q", cells[26].bitmap],
    ["r", cells[27].bitmap],
    ["s", cells[28].bitmap],
    ["t", cells[29].bitmap],
    ["u", cells[30].bitmap],
    ["v", cells[31].bitmap],
    ["w", cells[32].bitmap],
    ["x", cells[33].bitmap],
    ["y", cells[34].bitmap],
    ["z", cells[35].bitmap],
    ["A", cells[36].bitmap],
    ["B", cells[37].bitmap],
    ["C", cells[38].bitmap],
    ["D", cells[39].bitmap],
    ["E", cells[40].bitmap],
    ["F", cells[41].bitmap],
    ["G", cells[42].bitmap],
    ["H", cells[43].bitmap],
    ["I", cells[44].bitmap],
    ["J", cells[45].bitmap],
    ["K", cells[46].bitmap],
    ["L", cells[47].bitmap],
    ["M", cells[48].bitmap],
    ["N", cells[49].bitmap],
    ["O", cells[50].bitmap],
    ["P", cells[51].bitmap],
    ["Q", cells[52].bitmap],
    ["R", cells[53].bitmap],
    ["S", cells[54].bitmap],
    ["T", cells[55].bitmap],
    ["U", cells[56].bitmap],
    ["V", cells[57].bitmap],
    ["W", cells[58].bitmap],
    ["X", cells[59].bitmap],
    ["Y", cells[60].bitmap],
    ["Z", cells[61].bitmap],
    ["-", cells[62].bitmap],
    ["_", cells[63].bitmap],
    ["=", cells[64].bitmap],
    ["+", cells[65].bitmap],
    ["{", cells[66].bitmap],
    ["}", cells[67].bitmap],
    ["[", cells[68].bitmap],
    ["]", cells[69].bitmap],
    ["|", cells[70].bitmap],
    ["(", cells[71].bitmap],
    [";", cells[72].bitmap],
    [":", cells[73].bitmap],
    ["'", cells[74].bitmap],
    [")", cells[75].bitmap],
    [",", cells[76].bitmap],
    ["<", cells[77].bitmap],
    [">", cells[78].bitmap],
    ["/", cells[79].bitmap],
  )

  setCells(maps)
}

let drifter = false


function drawPixel(x, y, char) {
  cells[Math.floor(x / 16) + Math.floor(y / 16) * 10]?.setPixel(x % 16, y % 16, char)
}

function drawBitmap(bitmap, x, y) {
  bitmap = bitmap.replaceAll("\n", "")
  for (let i = 0; i < bitmap.length; i++) {
    if (bitmap.charAt(i) != '.')
      drawPixel(x + i % 16, y + Math.floor(i / 16), bitmap.charAt(i))
  }
}

function drawScreenSpaceBitmap(bitmap, x, y) {
  x = x - xDrift + 64
  if (x > -16) {
    drawBitmap(bitmap, x, y)
  }
}

function rotateImage(bitmap, angle) {
  angle += Math.PI / 2
  bitmap = bitmap.replaceAll("\n", "")
  out = []
  for (let y = 0; y < 16; y++) {
    for (let x = 0; x < 16; x++) {
      let rotated = new Vector(Math.cos(angle) * (y - 8) + Math.sin(angle) * (x - 8),
        -Math.sin(angle) * (y - 8) + Math.cos(angle) * (x - 8)
      ).plus(new Vector(8, 8)).toInt()

      out[y * 16 + x] =
        rotated.x >= 0 && rotated.x < 16 &&
        rotated.y >= 0 && rotated.y < 16 ?
        bitmap.charAt(rotated.y * 16 + rotated.x) :
        "."
    }
  }

  let outer = ""
  for (let i = 0; i < 16; i++) {
    for (let j = 0; j < 16; j++) {
      outer += out[i * 16 + j] ?? "."
    }
    outer += "\n"
  }
  return outer
}

function modAngle(angle) {
  return ((angle % (2 * Math.PI)) + (2 * Math.PI)) % (2 * Math.PI)
}

function clamp(num, min, max) {
  return Math.min(Math.max(num, min), max)
}

class Vector {
  constructor(x, y) {
    this.x = x
    this.y = y
  }

  static fromRotation(theta) {
    return new Vector(Math.cos(theta), Math.sin(theta))
  }

  plus(force) {
    return new Vector(this.x + force.x, this.y + force.y)
  }

  times(scalar) {
    return new Vector(this.x * scalar, this.y * scalar)
  }

  mod(num) {
    return new Vector(this.x % num, this.y % num)
  }

  getDst() {
    return Math.sqrt(this.dotProduct(this))
  }

  getAngle() {
    return Math.atan2(this.y, this.x)
  }

  normalize() {
    let dst = this.getDst()
    return new Vector(this.x != 0 ? this.x / dst : 0,
      this.y != 0 ? this.y / dst : 0)
  }

  toInt() {
    return new Vector(Math.round(this.x), Math.round(this.y))
  }

  dotProduct(other) {
    return this.x * other.x + this.y * other.y
  }

  swap() {
    return new Vector(this.y, this.x)
  }

  abs() {
    return new Vector(Math.abs(this.x), Math.abs(this.y))
  }

  clamp(max) {
    let speed = this.getDst()
    if (speed > max) {
      return new Vector((this.x / speed) * max, this.y / speed * max)
    }
    return this
  }

  toString() {
    return `${this.x}, ${this.y}`
  }
}

//TODO splits
function drawArrow(pos, rot, scale, flipped) {
  let i = 0;
  let cos = Math.cos(rot),
    sin = Math.sin(rot)
  for (; i < scale - 1; i++) {
    drawBitmap(rotateImage(flipped ?
        bitmap`
................
................
................
................
................
................
................
3333333333333333
3333333333333333
3333333333333333
................
................
................
................
................
................` :
        bitmap`
................
................
................
................
................
................
................
7777777777777777
7777777777777777
7777777777777777
................
................
................
................
................
................`, rot), Math.round(pos.x + i * 15 * cos),
      112 - Math.round(pos.y + i * 15 * sin))
  }
  drawBitmap(rotateImage(flipped ?
      bitmap`
................
................
................
................
..........3.....
..........33....
..........333...
33333333333333..
333333333333333.
33333333333333..
..........333...
..........33....
..........3.....
................
................
................` :
      bitmap`
................
................
................
................
..........7.....
..........77....
..........777...
77777777777777..
777777777777777.
77777777777777..
..........777...
..........77....
..........7.....
................
................
................`, rot), Math.round(pos.x + i * 15 * cos),
    112 - Math.round(pos.y + i * 15 * sin))
}

function drawLine(initial, final, color) {
  let slope = final.y - initial.y / final.x - initial.x
  for (let i = initial.x; i < final.x; i++) {
    drawPixel(initial.x + i, (i * slope + initial.y), color)
  }
}

class CircleCollider {
  constructor(pos, radius) {
    this.pos = pos
    this.radius = radius
  }

  isColliding(other) {
    let center = this.pos.plus(other.pos.times(-1))
    let xDist = Math.cos(center.getAngle()) * (this.radius + other.radius)
    return Math.abs(center.x) < Math.abs(xDist)
  }
}

let help = false
class Player {
  translateSpeed = 0.1
  rotSpeed = 0.025

  maxSpeed = 5
  maxAccel = this.translateSpeed * 5
  maxRot = this.rotSpeed * 5
  friction = 0.9
  driftion = 0.85

  driftStart = 0.15
  driftStop = 0.10

  skidMax = 10000

  isDrifting = false
  inputActive = true

  colliderFront = new CircleCollider(null, 2)
  colliderBack = new CircleCollider(null, 2)

  centering = 64

  played = false

  constructor(sprite) {
    this.pos = new Vector(0, 56)
    this.lastPos = this.pos
    this.velocity = new Vector(0, 0)
    this.acceleration = 0
    this.rotation = 0
    this.rotVel = 0

    this.sprite = sprite

    this.lastPoses = []
  }

  addForce(force) {
    if (this.inputActive) {
      this.acceleration += force
      this.acceleration = clamp(this.acceleration, -this.maxAccel, this.maxAccel)
    }
  }

  rotate(angle) {
    if (this.inputActive) {
      this.rotVel += angle
      this.rotVel = clamp(this.rotVel, -this.maxRot, this.maxRot)
    }
  }

  drawPointer(num, rot, mult, offset) {
    if (Math.abs(num) < 0.01) return
    let flipped = false
    if (num < 0) {
      num *= -1
      rot += Math.PI
      flipped = true
    }

    drawArrow(new Vector(this.toScreen(this.pos.x), this.pos.y)
      .plus(Vector.fromRotation(this.rotation + rot).times(offset)),
      this.rotation + rot,
      num * mult - 0.1,
      flipped)
  }

  calcDrifting() {
    this.isDrifting = Math.abs(this.rotVel * this.velocity.getDst()) > (this.isDrifting ?
        this.driftStop :
        this.driftStart) ||
      drifter
  }

  calcSkidStuff(forward) {
    let skidFront = forward.times(3)
    let skidDst = Vector.fromRotation(this.rotation - Math.PI / 2).times(5)
    let center = this.pos.plus(new Vector(8, -8))

    let back = center.plus(skidFront.times(-1))
    let front = center.plus(skidFront)
    //bl br fl fr
    return [
      back.plus(skidDst).toInt(),
      back.plus(skidDst.times(-1)).toInt(),
      front.plus(skidDst).toInt(),
      front.plus(skidDst.times(-1)).toInt(),
    ]
  }

  drawSkids() {
    if (this.lastPoses.length > this.skidMax) {
      this.lastPoses.shift(this.lastPoses.length - this.skidMax)
    }
    for (let i = 0; i < this.lastPoses.length - 1; i++) {
      // drawPixel(this.lastPoses[i].posFR.x, 112 - this.lastPoses[i].posFR.y, "0")

      this.lastPoses[i].forEach(x => {
        if (x.x - xDrift + 64 > 0)
          drawPixel(x.x - xDrift + 64, 112 - x.y, "0")
      })
    }
  }

  applyFriction(sign) {
    let up = new Vector(sign, sign).times(this.velocity.getDst())
  }

  checkCollider(collider) {
    return obstacles.some(x => collider.isColliding(x.collider));
  }

  calcCollision(forward, sign) {
    let futurePos = this.pos.plus(new Vector(8, 8))
      .plus(Vector.fromRotation(this.rotation + this.rotVel)
        .times(this.velocity.getDst()).times(sign))
    this.colliderFront.pos = futurePos.plus(forward.times(2))
    this.colliderBack.pos = futurePos.plus(forward.times(-2))
    // circle(this.colliderBack.pos, this.colliderBack.radius * 10)
    if (this.checkCollider(this.colliderBack) ||
      this.checkCollider(this.colliderFront) ||
      futurePos.y > 128 ||
      futurePos.y < 0 ||
      futurePos.x < 0) {
      this.velocity = new Vector(0, 0)
      if (!this.played) {
        playTune(tune`
750: G5-750 + F5-750,
23250`)
        this.played = true
      }
      // this.velocity = this.velocity.times(this.velocity.getDst() > 1.25 ? -0.4 : 0)
    }
  }

  tick() {
    let forward = Vector.fromRotation(this.rotation)

    this.calcDrifting()

    this.velocity = this.velocity.plus(forward.times(this.acceleration))
    this.velocity = this.velocity.times(this.isDrifting ? this.driftion : this.friction)
    this.velocity = this.velocity.clamp(this.maxSpeed)

    let sign = Math.sign(this.velocity.dotProduct(forward))

    this.calcCollision(forward, sign)

    this.pos = this.pos.plus(this.velocity)


    //TODO scale rotation by velocity
    if (this.velocity.getDst() > 0.1) {
      this.rotation += this.rotVel * (sign < 1 ? -1 : 1)
      this.rotation = modAngle(this.rotation)
      if (!this.isDrifting) {
        this.velocity = forward
          .times(this.velocity.getDst())
          .times(sign)
      }
    }

    if (this.isDrifting) {
      this.lastPoses.push(this.calcSkidStuff(forward))
      playTune(tune`
500: D4/500,
15500`)
    }
  }

  toScreen(x) {
    return this.pos.x > 64 ? x - xDrift + this.centering : x
  }

  draw() {
    drawBitmap(rotateImage(this.sprite, this.rotation),
      this.toScreen(Math.round(this.lastPos.x)),
      Math.round(112 - this.lastPos.y))
  }

  drawArrows() {
    this.drawPointer(this.acceleration, 0, 1 / this.translateSpeed, 8)
    this.drawPointer(this.rotVel, Math.PI / 2, 1 / this.rotSpeed, 8)
  }

  updatePos() {
    this.played = false

    if (this.pos.x > 64) xDrift = Math.round(this.pos.x)
    this.lastPos = this.pos
  }
}
const player = new Player(bitmap`
................
................
................
..0000.....0000.
.LLLLLLLLLLLLLLL
LLLLL777LLLLLLLL
LLLLL777LLLLLLLL
1111177711111111
1111177711111111
LLLLL777LLLLLLLL
LLLLL777LLLLLLLL
.LLLLLLLLLLLLLLL
..0000.....0000.
................
................
................`)

function circle(pos, radius) {
  pos = new Vector(pos.x / 2, pos.y)
  pos = pos.toInt()
  for (let x = 0; x < radius * 2; x++) {
    drawPixel(x + pos.x, 128 - Math.floor(Math.sqrt(radius - Math.pow(x - pos.x, 2)) + pos.y), help ? "4" : "5")
  }
  for (let x = 0; x < radius * 2; x++) {
    drawPixel(x + pos.x, 128 - Math.floor(-Math.sqrt(radius - Math.pow(x - pos.x, 2)) + pos.y), help ? "4" : "5")
  }
}


let timer = 3
let going = false

let last = -1

function drawTimer() {
  clearText()
  const rounded = Math.round(timer)
  addText(rounded.toString(), {
    x: 18,
    y: 15
  })
  if (rounded != last) {
    playTune(tune`
500: F5^500,
15500`)
  }
  last = rounded
}

let end = false
let levelEnd = 1000

function checkGoingEnd() {
  if (timer <= 0) {
    if (player.pos.x > levelEnd) {
      end = true
    }
    timer = 3
    going = false
    player.updatePos()
    player.inputActive = true
  }
}

function blackScreen() {
  cells.forEach(x => x.bitmap = bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`)
  setScreen()
}

function checkNotGoingEnd() {
  if (timer <= 0) {
    timer = 1
    going = true
    blackScreen()
    setTimeout(() => player.inputActive = false, 300)
  }
}

class Obstacle {
  constructor(pos, radius, sprite) {
    this.pos = pos
    this.collider = new CircleCollider(pos.plus(new Vector(8, 8)), radius)
    this.sprite = sprite
  }

  draw() {
    //this is for performance I promise
    if (xDrift > this.pos.x - 80 && xDrift < this.pos.x + 80)
      drawScreenSpaceBitmap(this.sprite, this.pos.x, 112 - this.pos.y)
  }
}

const cone = bitmap`
................
................
.....99999......
....9999999.....
...999222999....
..99929992999...
..99299999299...
..99299999299...
..99299999299...
..99929992999...
...999222999....
....9999999.....
.....99999......
................
................
................`
let obstacles = [
  //first
  new Obstacle(new Vector(128, 56), 11, bitmap`
....CCCCCCCC....
...CCCCCCCCCC...
..CCCCCCCCCCCC..
.CCCCCCCCCCCCCC.
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
.CCCCCCCCCCCCCC.
..CCCCCCCCCCCC..
...CCCCCCCCCC...
....CCCCCCCC....`),

  //cones
  new Obstacle(new Vector(300, 23), 11, cone),
  new Obstacle(new Vector(300, 87), 11, cone),
  
  new Obstacle(new Vector(375, 13), 11, cone),
  new Obstacle(new Vector(375, 77), 11, cone),
  
  new Obstacle(new Vector(450, 13), 11, cone),
  new Obstacle(new Vector(450, 77), 11, cone),
  
  new Obstacle(new Vector(525, 23), 11, cone),
  new Obstacle(new Vector(525, 87), 11, cone),
  
  new Obstacle(new Vector(600, 33), 11, cone),
  new Obstacle(new Vector(600, 97), 11, cone),

  new Obstacle(new Vector(700, 64), 11, cone),
  new Obstacle(new Vector(750, 96), 11, cone),
]

function setKeys() {

  onInput("w", () => player.addForce(player.translateSpeed))
  onInput("s", () => player.addForce(-player.translateSpeed))

  onInput("a", () => player.rotate(player.rotSpeed))
  onInput("d", () => player.rotate(-player.rotSpeed))

  onInput("k", () => drifter = !drifter)
}

let frameSpeed = 1000 / 30
let totalTime = 0

function setup() {
  setScreen()

  let out = ""
  for (let i = 0; i < 8; i++) {
    for (let j = 0; j < 10; j++) {
      out += cells[i * 10 + j].char
    }
    out += "\n"
  }
  setMap(out)
}

function tick() {
  const start = performance.now()
  if (end) {
    showTime()
    return
  }

  if (going) {
    player.tick()

    // smear effect
    // player.draw()

    checkGoingEnd()
  } else {
    player.drawSkids()

    // player.tick()
    // player.updatePos()
    player.draw()
    player.drawArrows()
    setScreen()
    drawTimer()
    checkNotGoingEnd()
    
    obstacles.forEach(o => o.draw())
  }

  totalTime += frameSpeed / 1000
  timer -= frameSpeed / 1000

  // help why isn't this actually doing the thing
  setTimeout(tick, frameSpeed - (performance.now() - start))
}

function showTime() {
  blackScreen()
  addText("Your Time Was:", { x: 3, y: 7 })
  addText(totalTime.toLocaleString('en-US', {
    minimumIntegerDigits: 4,
    minimumFractionDigits: 4,
    useGrouping: false
  }), { x: 5, y: 8 })
}

setup()
setCells(titles)
setScreen()
let titleY = 10
let titleColor = color`D`
addText("press any key", { y: titleY, color: titleColor })
addText("to start", { y: titleY + 1, color: titleColor })
let started = false
let tutorialed = false
afterInput(() => {
  if (!started) {
    started = true
    xDrift = 64
    blackScreen()
    clearText()
    const start = 0
    const colory = color`2`
    const texts = [
      "W holds gas",
      "and S releases it",
      "Press A to turn left",
      "and D to turn right",
      "",
      "After staying awake",
      "for three seconds,", "you will fall asleep",
      "and your car will",
      "continue by itself",
      "",
      "Avoid the obstacles,",
      "press K to drift,",
      "and get home as fast",
      "as possible.",
      "Good luck",
    ]
    for (let i = 0; i < texts.length; i++) {
      addText(texts[i], { y: start + i, color: colory })
    }
  } else if (!tutorialed) {
    tutorialed = true
    setKeys()
    tick()
  }
})
